package com.one.common.manager;


import android.app.Activity;
import android.os.Handler;

import com.alibaba.android.arouter.launcher.ARouter;
import com.one.common.model.extra.BaseExtra;
import com.one.common.utils.Logger;
import com.one.common.view.base.BaseActivity;

/**
 * creator: zhulunjun
 * time:    2018/6/13
 * describe: 路由控制
 */

public class RouterManager {

    private static RouterManager instance;

    public static RouterManager getInstance() {
        if (instance == null) {
            synchronized (RouterManager.class) {
                if (instance == null) {
                    instance = new RouterManager();
                }
            }
        }
        return instance;
    }

    /**
     * 默认带返回值跳转
     */
    public final static int RESULT_REQUEST_CODE_DEFAULT = 999;
    public final static int RESULT_REQUEST_CODE_CLOSE = 555;

    /**
     * 页面跳转
     */
    public <E extends BaseExtra> void go(Activity context, String path, E extra) {
        Logger.d("ARouter", "无参数跳转 " + path);
        if (extra != null) {
            ARouter.getInstance().build(path)
                    .withSerializable(E.getExtraName(), extra)
                    .navigation(context);
        } else {
            ARouter.getInstance().build(path)
                    .navigation(context);
        }


    }

    public void go(BaseActivity context, String path) {
        go(context, path, null);
    }

    /**
     * 跳转页面并关闭当前页面
     */
    public void goAndFinish(BaseActivity context, String path) {
        go(context, path);
        close(context);
    }

    /**
     * 跳转页面并关闭当前页面
     */
    public <E extends BaseExtra> void goAndFinish(BaseActivity context, String path, E extra) {
        go(context, path, extra);
        close(context);
    }

    private void close(BaseActivity context){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if(context!=null){
                    context.finishPage();
                }
            }
        }, 1000);
    }

    public void go(String path) {
        go(null, path);
    }

    /**
     * 带参数的跳转
     */
    public <E extends BaseExtra> void go(String path, E extra) {
        E.setExtraName(BaseExtra.nextExtraName());
        ARouter.getInstance().build(path)
                .withSerializable(E.getExtraName(), extra)
                .navigation();
    }

    /**
     * 带返回值的跳转
     */
    public void goForResult(Activity context, String path, int requestCode) {
        Logger.d("ARouter", "无参数跳转 " + path);
        ARouter.getInstance().build(path).navigation(context, requestCode);
    }

    /**
     * 带默认code的带返回值的跳转
     */
    public void goForResult(Activity context, String path) {
        goForResult(context, path, RESULT_REQUEST_CODE_DEFAULT);
    }

    /**
     * 带返回值的跳转
     * 带参数
     */
    public <E extends BaseExtra> void goForResult(Activity context, String path, int requestCode, E extra) {
        Logger.d("ARouter", "带参数跳转 " + path);
        ARouter.getInstance()
                .build(path)
                .withSerializable(E.getExtraName(), extra)
                .navigation(context, requestCode);
    }

    /**
     * 带默认code的带返回值的跳转
     * 带参数
     */
    public <E extends BaseExtra> void goForResult(Activity context, String path, E extra) {
        goForResult(context, path, RESULT_REQUEST_CODE_DEFAULT, extra);
    }
}
